package com.jianyun.jyzs.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import com.jianyun.jyzs.R;

import java.util.List;

public class RadioDialog extends Dialog implements View.OnClickListener{
	
	public Context context;
	public String title;
	public List<String[]> message;
	public String positiveButtonText;
	public String negativeButtonText;
	private RadioDialogListener listener; 
	private RadioDialogAdapter adapter;
	 
    public interface RadioDialogListener{ 
        public void onClick(View view, int position);
    } 
    
   	public RadioDialog(Context context) {
   		super(context);
   	}
    
   	public RadioDialog(Context context, int id) {
   		super(context,id);
   	}
    public RadioDialog(Context context, String title, List<String[]> message, String positive, String negative, RadioDialogListener listener) {
    	super(context, R.style.fullDialog);
    	this.context = context;
    	this.title = title;
    	this.message = message;
    	this.positiveButtonText = negative;
    	this.negativeButtonText = positive;
    	this.listener = listener;
    } 

	@Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.dialog_radio_layout, null);
        setContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
        init();
    } 
	public void init() {
		//点击屏幕不关闭dialog
		setCancelable(false);
		((TextView) findViewById(R.id.title)).setText(title);
		Button positiveButton = (Button) findViewById(R.id.positiveButton);
		Button negativeButton = (Button) findViewById(R.id.negativeButton);

		if (positiveButtonText != null) {
			positiveButton.setText(positiveButtonText);
			positiveButton.setOnClickListener(this);
		} else {
			positiveButton.setVisibility(View.GONE);
		}
		if (negativeButtonText != null) {
			negativeButton.setText(negativeButtonText);
			negativeButton.setOnClickListener(this);
		} else {
			negativeButton.setVisibility(View.GONE);
		}
		if (message != null) {
			ListView list = (ListView) findViewById(R.id.list);
			adapter = new RadioDialogAdapter(context, message);
			list.setAdapter(adapter);
		}
	}

	@Override
	public void onClick(View v) {
		listener.onClick(v,adapter.getItemId());
		dismiss();
	}
}
